package com.central.apps.handler;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Component
public class queryHandler extends BaseHandler {

    @Autowired private HandlerService handlerService;

    @Override
    public void addDefault(String app, String object, Map<String, Object> defaultData) throws Exception {
            List<Map<String, Object>> bySql = handlerService.findBySql("select Year(now()) as year,Month(now()) as month");
            defaultData.put("year", MapUtils.getString(bySql.get(0),"year"));
            defaultData.put("month", MapUtils.getString(bySql.get(0),"month"));
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if("queryApp".equals(app)){
//            if("gq_query".equals(objName)){
//                //调整年月格式
//                String id = MapUtils.getString(data, "id");
//                String year = MapUtils.getString(data, "yearnew");//2023-05-12
//                String yearnew = year.substring(0, year.indexOf("-"));//年
//                String monthnew = year.substring(year.indexOf("-")+1, year.indexOf("-",year.indexOf("-")+1));//月
//                Integer yearInt = Integer.valueOf(yearnew);
//                Integer monthInt = Integer.valueOf(monthnew);
//                handlerService.findBySql("update gq_query set year =" + yearInt + " , month = " +monthInt + " where id = '" + id + "'");
//            }
        }
        String id =MapUtils.getString(data,"id");
        List<Map<String, Object>> bySql = handlerService.findBySql("select endnum,endmoney from gq_queryline where queryid ='" + id + "'");
        BigDecimal totalnumber = new BigDecimal("0");
        BigDecimal totalcost = new BigDecimal("0");
        for(Map<String, Object> map :bySql){
            Float endnum =MapUtils.getFloat(map,"endnum");
            String endmoney =MapUtils.getString(map,"endmoney");
            BigDecimal endmoney1 = new BigDecimal(endmoney);
            BigDecimal bigDecimal = new BigDecimal("0");
            if(endnum ==null){endnum=(float)0;}
            BigDecimal endnum1 = new BigDecimal(endnum);
            totalnumber =totalnumber.add(endnum1);
            if(endmoney1 ==null){endmoney1=bigDecimal;}
            totalcost =totalcost.add(endmoney1);
        }
        handlerService.updateBySql("update gq_query set totalnumber ="+totalnumber+",totalcost ="+totalcost+" where id ='"+id+"'");
    }

    @Override
    public void beforDelete(String app, String objName, Long id, boolean isLogic) {
        if ("gq_queryline".equals(objName)){
            List<Map<String, Object>> bySql = handlerService.findBySql("select endnum,endmoney,queryid from gq_queryline where id ='" + id + "'");

            List<Map<String, Object>> bySql1 = handlerService.findBySql("select endnum,endmoney from gq_queryline where queryid ='" + MapUtils.getString(bySql.get(0),"queryid") + "'");
            Integer totalnumbertotal =0;
            Integer totalcosttotal =0;

            if(CollectionUtils.isNotEmpty(bySql1)){
                for (Map<String, Object> map : bySql1) {
                    Integer totalnumber =MapUtils.getInteger(map,"endnum");
                    totalnumbertotal = totalnumbertotal + totalnumber;
                    Integer totalcost =MapUtils.getInteger(map,"endmoney");
                    totalcosttotal = totalcosttotal + totalcost;
                }
            }
            if(CollectionUtils.isNotEmpty(bySql)){
                Map<String, Object> map = bySql.get(0);
                Integer totalnumber =MapUtils.getInteger(map,"endnum");
                Integer totalcost =MapUtils.getInteger(map,"endmoney");
                int totalnumbernew = totalnumbertotal - totalnumber;
                int totalcostnew = totalcosttotal - totalcost;
                handlerService.updateBySql("update gq_query set totalnumber ="+totalnumbernew+",totalcost ="+totalcostnew+" where id ='"+MapUtils.getString(bySql.get(0),"queryid")+"'");
            }
        }


    }
}
